<?php

namespace Bigspender\DefaultBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Bigspender\DefaultBundle\Entity\SignUp;

/**
*
* @ORM\Table(name="`user`")
* @ORM\Entity(repositoryClass="Bigspender\DefaultBundle\Entity\UserRepository")
*/
class User extends Row implements AdvancedUserInterface, \Serializable
{
	/**
	* @ORM\Column(type="string", length=25, unique=true)
	*/
	private $username;

	/**
	* @ORM\Column(type="string", length=64)
	*/
	private $password;

	/**
	* @ORM\Column(type="string", length=60, unique=true, nullable=true)
	*/
	private $email;

	/**
	* @ORM\Column(type="string", length=256, unique=true)
	*/
	private $hash;

	/**
	* @ORM\Column(name="active", type="boolean")
	*/
	private $active;

	public function __construct()
	{
		$this->active = true;
		parent::__construct();
	}

	public function getUsername()
	{
		return $this->username;
	}

	public function getSalt()
	{
		return null;
	}

	public function getPassword()
	{
		return $this->password;
	}

	public function getRoles()
	{
		return array('ROLE_USER', 'ROLE_ADMIN');
	}

	public function eraseCredentials()
	{

	}

	public function isEnabled()
	{
		return $this->active;
	}

	public function isAccountNonExpired() 
	{
		return true;
	}

	public function isAccountNonLocked()
	{
		return true;
	}

	public function isCredentialsNonExpired()
	{
		return true;
	}

	public function serialize()
	{
		return serialize(
			array(
				$this->id,
				$this->username,
				$this->password,
			)
		);
	}

	public function unserialize($serialized)
	{
		list (
			$this->id,
			$this->username,
			$this->password,
		) = unserialize($serialized);
	}

	public function setFromSignUp(SignUp $signup) 
	{
		$this->username = $signup->getName();
		$this->email 	= $signup->getEmail();
		$this->password = $signup->getPassword();
		$this->active 	= false;
	}

	public function setHash($hash)
	{
		$this->hash = $hash;
	}

	public function getHash()
	{
		return $this->hash;
	}

	public function activate()
	{
		$this->active = true;
		$this->hash = $this->id;
	}
}